package com.develophelper.example.swagger.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

/**
 * @author snaildev
 * @date 2020/12/07
 **/
@Configuration
@EnableOpenApi
public class SwaggerBootstrapConfig {

    @Bean
    public Docket docket(Environment environment) {

        Profiles dev = Profiles.of("dev");
        boolean flag = environment.acceptsProfiles(dev);

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("snaildev")
                // enable()是否启用Swagger，默认是true
                .enable(flag)
                .select()
                // RequestHandlerSelectors,配置要扫描接口的方式
                // basePackage指定要扫描的包
                // any()扫描所有，项目中的所有接口都会被扫描到
                // none()不扫描
                // withClassAnnotation()扫描类上的注解
                // withMethodAnnotation()扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.tioxy.controller"))
                // paths()过滤某个路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("snaildev", "blog.develophelper.com", "snaildev@outlook.com");
        return new ApiInfo(
                "snaildev的接口文档",
                "项目描述",
                "1.0",
                "http://blog.develophelper.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
